using System.Collections;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
using static RoleController;

public class TeamController : SerializedMonoBehaviour
{
    [Header("Spawn")]
    public Dictionary<GameObject, int> prefabs;
    public int spawnMin;
    public int spawnMax;
    public bool startSpawn = true;
    public Transform spawnPositionRoot;
    private int _allWeight = -1;
    public int spawnCd;

    [Header("Roles")]
    public List<RoleController> listRoles;
    public RoleCamp camp;

    [Header("Fight")]
    public string fightId;
    public AlertBehaviour alert;
    // Start is called before the first frame update
    private void Start()
    {
        if (startSpawn)
        {
            Spawn();
        }
    }

    public void Spawn()
    {
        int r = Random.Range(spawnMin, spawnMax);
        for (int i = 0; i < r; i++)
        {
            GameObject prefab = GetPrefab();
            Transform tfBorn = GetBornTransform();
            GameObject obj = Instantiate<GameObject>(prefab, tfBorn.position, tfBorn.rotation);
            RoleController role = obj.GetComponent<RoleController>();
            if (role != null)
            {
                role.team = this;
                role.camp = camp;
                if (alert != null)
                {
                    role.alert = alert;
                }
                listRoles.Add(role);
            }
        }
    }
    protected Transform GetBornTransform()
    {
        if (spawnPositionRoot.childCount == 0)
            return spawnPositionRoot;

        return spawnPositionRoot.GetChild(Random.Range(0, spawnPositionRoot.childCount));
    }

    protected GameObject GetPrefab()
    {
        if (_allWeight == -1)
        {
            _allWeight = 0;
            foreach (var item in prefabs)
            {
                _allWeight += item.Value;
            }
        }

        int r = Random.Range(0, _allWeight);

        foreach (var item in prefabs)
        {
            if (r < item.Value)
            {
                return item.Key;
            }
            r -= item.Value;
        }

        return null;
    }

    public void Fight(TeamController team)
    {

        if (string.IsNullOrEmpty(fightId))
        {
            if (string.IsNullOrEmpty(team.fightId))
            {
                fightId = FightManager.GetFightId();
                team.fightId = fightId;
                team.EnterFight();
            }
            else
            {
                fightId = team.fightId;
            }

            EnterFight();
        }
        else if (string.IsNullOrEmpty(team.fightId))
        {
            team.fightId = fightId;
            team.EnterFight();
        }
        else
        {
            fightId = FightManager.instance.MergeFight(fightId, team.fightId);
        }
    }



    public void EnterFight()
    {
        FightManager.instance.AddFight(fightId, listRoles);

        for (int i = 0; i < listRoles.Count; i++)
        {
            if (listRoles[i].state == RoleState.Idle)
            {
                listRoles[i].FightEnter();
            }
        }
    }
    // Update is called once per frame
    private float time;
    void Update()
    {
        if (listRoles.Count == 0)
        {
            time += Time.deltaTime;
            if (time >= spawnCd)
            {
                fightId = null;
                Spawn();
                time = 0;
            }
        }
    }
}
